package com.fpost.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.fpost.dao.GenericDAO;
import com.fpost.domain.ArticleRating;

public class ArticleRatingDAO extends GenericDAO<ArticleRating> {
	
	@Override
	public Long save(ArticleRating articleRating) throws HibernateException {
		Long articleRatingId = null;
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			articleRatingId = (Long) session.save(articleRating);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw e;
		} finally {
			session.close();
		}
		return articleRatingId;
	}

	@Override
	public void delete(long id) throws HibernateException {
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.delete(get(id));
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw e;
		} finally {
			session.close();
		}

	}

	@Override
	public void delete(ArticleRating articleRating) throws HibernateException {
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.delete(articleRating);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw e;
		} finally {
			session.close();
		}

	}

	@Override
	public ArticleRating get(long id) throws HibernateException {
		Session session = getSessionFactory().openSession();
		ArticleRating articleRating = null;
		try {
			session.beginTransaction();
			articleRating = (ArticleRating) session.get(ArticleRating.class, id);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw e;
		} finally {
			session.close();
		}
		return articleRating;
	}


	@Override
	public void saveOrUpdate(ArticleRating articleRating) throws HibernateException {
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.saveOrUpdate(articleRating);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw e;
		} finally {
			session.close();
		}

	}
	
	@SuppressWarnings("unchecked")
	public List<ArticleRating> getAll() throws HibernateException {
		Session session = getSessionFactory().openSession();
		List<ArticleRating> articleRatings;
		try {
			articleRatings = (List<ArticleRating>) session.createCriteria(ArticleRating.class).list();
		} catch (HibernateException e) {
			e.printStackTrace();
			throw e;
		}
		return articleRatings;		
	}
	
	@SuppressWarnings("unchecked")
	public List<ArticleRating> getRatingsByArticleId(long articleId) throws HibernateException {
		Session session = getSessionFactory().openSession();
		List<ArticleRating> articleRatings;
		try {
			articleRatings = (List<ArticleRating>)session.createCriteria(ArticleRating.class)
					.add(Restrictions.eq("article.articleId", articleId))
					.list();
		} catch (HibernateException e) {
			e.printStackTrace();
			throw e;
		}
		return articleRatings;		
	}
}
